home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / UNIQUE.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  2KB  |  60 lines

  1. /* unique.c */
  2.  
  3. #include <windows.h>
  4. #include <time.h>
  5. #include "unique.h"
  6.  
  7. static LPSTR Months[12] = {"JAN","FEB","MAR","APR","MAY","JUN",
  8.                            "JUL","AUG","SEP","OCT","NOV","DEC"};
  9.  
  10. /*
  11. ** Encode [0..50653] as 3 base-37 digits.
  12. ** The base-37 char set is [0..9,@,A..Z].
  13. */
  14.  
  15. void Base37(unsigned int Value, LPSTR Buffer)
  16. {int Digit;
  17.  /* compute coefficient of 37^2 */
  18.  Digit = Value / (37*37);
  19.  if(Digit<=9)  *Buffer++ = '0' + Digit;
  20.  else *Buffer++ = '@' + (Digit-10);
  21.  Value -= (37*37*Digit);
  22.  /* compute coefficient of 37^1 */
  23.  Digit = Value / 37;
  24.  if(Digit<=9)  *Buffer++ = '0' + Digit;
  25.  else *Buffer++ = '@' + (Digit-10);
  26.  Value -= (37*Digit);
  27.  /* compute coefficient of 37^0 */
  28.  Digit = Value;
  29.  if(Digit<=9)  *Buffer++ = '0' + Digit;
  30.  else *Buffer++ = '@' + (Digit-10);
  31.  *Buffer = '\0';
  32. }
  33.  
  34. /* construct unique (one per sec) filename */
  35.  
  36. void unique(LPSTR Buffer)
  37. {unsigned int Value;
  38.  time_t TheTime;
  39.  struct tm *Ptr;
  40.  char AmPm;
  41.  char Temp[8];
  42.  /* get local time */
  43.  TheTime = time(NULL);
  44.  Ptr = localtime(&TheTime);
  45.  if(Ptr->tm_hour>12)
  46.    {AmPm = 'P';
  47.     Ptr->tm_hour -= 12;
  48.    }
  49.  else AmPm = 'A';
  50.  /* compute time value */
  51.  Value = (60*60) * (Ptr->tm_hour) + 60*(Ptr->tm_min) + (Ptr->tm_sec);
  52.  /* convert to printable base-37 */
  53.  Base37(Value, (LPSTR)Temp);
  54.  /* construct unique file name */
  55.  wsprintf((LPSTR)Buffer,"%d%s%d%c.%s", Ptr->tm_mday,
  56.           (LPSTR)Months[Ptr->tm_mon], Ptr->tm_year,
  57.           AmPm, (LPSTR)Temp);
  58. }
  59.                                                                                                                                                                          
  60.